import XLSX from 'xlsx'

function readFile(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.onload = (e) => resolve(new Uint8Array(e.target.result))
    reader.readAsArrayBuffer(file)
  })
}

export default async function (file) {
  const data = await readFile(file)
  const workbook = XLSX.read(data, {
    type: 'array',
  })

  const sheetList = workbook.Workbook.Sheets.map((x, index) => ({
    id: x.sheetId,
    name: x.name,
    index,
  }))
  const sheet = sheetList.map(x => workbook.Sheets[x.name]).map(x => XLSX.utils.sheet_to_json(x, {
    header: 'A',
    raw: false,
  }))

  return {
    file,
    workbook,

    sheetList,
    sheet,
  }
}
